package com.freshcity.page.home
{
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Component;
	import com.freshcity.component.container.HBox;
	import com.freshcity.core.BasePage;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.qq.SettingQQWin;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	import com.greensock.loading.LoaderMax;
	
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.controls.Label;
	import fl.controls.NumericStepper;
	import fl.data.DataProvider;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;

	public class SetFriendCond extends Component
	{
		public var base:BasePage;
		public var isRegister:Boolean;
		
		private var xml:XML=LoaderMax.getContent("dataXML");

		private var back:SettingQQWin;
		private var mainSprite:Sprite;
		private var ypos:Number;
		private var provinceBox:ComboBox;
		private var cityBox:ComboBox;
		private var yearFromBox:ComboBox;
		private var yearToBox:ComboBox;
		private var tallFromBox:ComboBox;
		private var tallToBox:ComboBox;
		private var degreeBox:ComboBox;
		private var incomeStepper:NumericStepper;
		private var friendStepper:NumericStepper;

		public function SetFriendCond(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this._width = 380;
			this._height = 300;
			
			super(parent, xpos, ypos);

			back.confirmBtn.addEventListener(MouseEvent.CLICK, onSubmit);
			back.closeBtn2.addEventListener(MouseEvent.CLICK, hide);
		}
		
		protected function onSubmit(event:MouseEvent):void
		{
			var obj:*={};
			obj.yearFrom = yearFromBox.selectedLabel;
			obj.yearTo = yearToBox.selectedLabel;
			if(obj.yearFrom=='请选择' || obj.yearTo=='请选择'){
				base.msg.showRollTipsMsg("请选择出生年份范围");
				return;
			}else if(int(obj.yearTo)<=int(obj.yearFrom)){
			  base.msg.showRollTipsMsg("终止年份应大于起始年份");
			  return;
			}
			
			if(provinceBox.enabled && cityBox.enabled){
				obj.addProv = provinceBox.selectedIndex > 0 ? provinceBox.selectedLabel:'';
				obj.addCity = cityBox.selectedIndex > 0 ? cityBox.selectedLabel:'';
			}
			
			if(this.incomeStepper.enabled){
				obj.income = this.incomeStepper.value;
			}
			
			if(this.friendStepper.enabled){
				obj.fvalue = this.friendStepper.value;
			}
			
			if(tallFromBox.enabled && tallToBox.enabled){
				obj.tallFrom = tallFromBox.selectedLabel;
				obj.tallTo = tallToBox.selectedLabel;
				if(obj.tallFrom=='请选择' || obj.tallTo=='请选择'){
					base.msg.showRollTipsMsg("请选择身高范围");
					return;
				}else if(int(obj.tallTo)<=int(obj.tallFrom)){
					base.msg.showRollTipsMsg("终止身高应大于起始身高");
					return;
				}
			}
			if(degreeBox.enabled){
				if(degreeBox.selectedLabel=='请选择'){
					base.msg.showRollTipsMsg("请选择学历要求");
					return;
				}
				obj.degree = degreeBox.selectedLabel;
			}
			
			if(isRegister){
				finishSetting(obj);
			}else{
				base.commu.send(OpCodes.SETTING_QQ_FRIEND, obj, onSettingCallback);
			}
		}
		
		private function onSettingCallback(data:*):void
		{
			if(data is String){
				base.msg.showRollTipsMsg(data);
			}else{
				base.cond = data;
				base.msg.showRollTipsMsg("成功完成操作");
				if(base.settingCallback!=null){
					base.settingCallback();
					this.destroy();
				}else{
					this.hide();
				}
			}
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new SettingQQWin();
			addChild(back);
		}

		private function provinceSelected(e:Event):void
		{
			var da:Array=[];
			var c:ComboBox=e.currentTarget as ComboBox;
			da.push({label: '不限'});
			for each (var x:XML in xml.cities.prov.(@label == c.selectedItem.label).children())
			{
				da.push({label: x.@value});
			}
			cityBox.dataProvider=new DataProvider(da);
		}

		override public function draw():void
		{
			super.draw();
			ypos=10.0;
			mainSprite=new Sprite();
			mainSprite.mouseEnabled=false;

			this.levelOne();
			this.levelTwo();
			this.levelThree();
			this.levelFour();
			this.levelFive();
			this.levelSix();
			this.levelSeven();

			back.panel.source=mainSprite;
		}
		
		private function setComboxItem(element:ComboBox, dataString:String):void {
			var array:Array = element.dataProvider.toArray();
			for (var i:Number=0; i < array.length; i++) {
				if(array[i].label == dataString) {
					element.selectedIndex = i;
					break;
				}
			}
		}		

		private function levelOne():void
		{
			var dataArray:Array=[];
			var x:XML;

			for each (x in xml.year.children())
			{
				dataArray.push({label: x.@value});
			}

			var box:HBox=new HBox();
			yearFromBox=new ComboBox();
			yearFromBox.dataProvider=new DataProvider(dataArray);
			box.addChild(yearFromBox);
			var lb:Label=new Label();
			lb.text=" - ";
			lb.width=30;
			box.addChild(lb);
			yearToBox=new ComboBox();
			yearToBox.dataProvider=new DataProvider(dataArray);
			box.addChild(yearToBox);
			box.initialize();

			setItem(box, "出生年份");

			if (base.cond.yearFrom){
				setComboxItem(yearFromBox,base.cond.yearFrom);
			}
			if (base.cond.yearTo){
				setComboxItem(yearToBox,base.cond.yearTo);
			}
		}

		private function levelTwo():void
		{
			var box:HBox=new HBox();
			provinceBox=new ComboBox();
			var da:Array=[];
			da.push({label: '不限'});
			for each (var x:XML in xml.cities.prov)
			{
				da.push({label: x.@label});
			}
			provinceBox.dataProvider=new DataProvider(da);
			provinceBox.addEventListener(Event.CHANGE, provinceSelected);
			
			box.addChild(this.provinceBox);
			cityBox=new ComboBox();
			box.addChild(this.cityBox);
			box.initialize();

			setItem(box, "居住地");

			if (int(base.user.level) >= 5)
			{
				if (base.cond.addProv){
					setComboxItem(provinceBox,base.cond.addProv);
					provinceBox.dispatchEvent(new Event(Event.CHANGE));
				}
				if (base.cond.addCity)
					setComboxItem(cityBox,base.cond.addCity);
			}
			else
			{
				this.provinceBox.enabled=false;
				this.cityBox.enabled=false;
			}
		}

		private function levelThree():void
		{
			var box:HBox=new HBox();
			var inLabel:TextField = StringUtil.getTextField("美誉度不低于");
			box.addChild(inLabel);
			
			incomeStepper=new NumericStepper();
			incomeStepper.stepSize = 1;
			incomeStepper.maximum = base.user.income;
			incomeStepper.minimum = 0;

			box.addChild(incomeStepper);
			box.initialize();

			setItem(box);

			if (int(base.user.level) >= 6)
			{
				if (base.cond.income)
				incomeStepper.value = base.cond.income;
			}
			else
			{
				incomeStepper.enabled=false;
			}
		}
		

		private function levelFour():void
		{
			var dataArray:Array=[];
			var x:XML;

			for each (x in xml.tall.children())
			{
				dataArray.push({label: x.@value});
			}

			var box:HBox=new HBox();
			tallFromBox=new ComboBox();
			tallFromBox.dataProvider=new DataProvider(dataArray);
			box.addChild(tallFromBox);
			var lb:Label=new Label();
			lb.text=" - ";
			lb.width=30;
			box.addChild(lb);
			tallToBox=new ComboBox();
			tallToBox.dataProvider=new DataProvider(dataArray);
			box.addChild(tallToBox);
			box.initialize();

			setItem(box, "身高");

			if (int(base.user.level) >= 7)
			{
				if (base.cond.tallFrom)
					setComboxItem(tallFromBox,base.cond.tallFrom);
				if (base.cond.tallTo)
					setComboxItem(tallToBox,base.cond.tallTo);
			}
			else
			{
				tallFromBox.enabled=false;
				tallToBox.enabled=false;
			}
		}
		
		private function levelFive():void
		{
			var box:HBox=new HBox();
			var inLabel:TextField = StringUtil.getTextField("友好度不低于");
			box.addChild(inLabel);
			
			friendStepper=new NumericStepper();
			friendStepper.stepSize = 1;
			friendStepper.maximum = 500;
			friendStepper.minimum = 0;
			
			box.addChild(friendStepper);
			box.initialize();
			
			setItem(box);
			
			if (int(base.user.level) >= 8)
			{
				if (base.cond.fvalue)
					friendStepper.value = base.cond.fvalue;
			}
			else
			{
				friendStepper.enabled=false;
			}
		}		

		private function levelSix():void
		{
			var dataArray:Array=[];
			var x:XML;

			for each (x in xml.degree.children())
			{
				dataArray.push({label: x.@value});
			}

			var box:HBox=new HBox();
			degreeBox=new ComboBox();
			degreeBox.dataProvider=new DataProvider(dataArray);
			box.addChild(degreeBox);
			box.initialize();

			setItem(box, "最低学历");

			if (int(base.user.level) >= 10)
			{
				if (base.cond.degree)
					setComboxItem(degreeBox,base.cond.degree);
			}
			else
			{
				degreeBox.enabled=false;
			}
		}


		private function levelSeven():void
		{
			var box:HBox=new HBox();
			var loc1:CheckBox=new CheckBox();
			loc1.label = "向申请者提问";
			box.addChild(loc1);
			box.initialize();

			setItem(box);
			loc1.enabled=false;
		}

		private function setItem(it:DisplayObject, label:String=null):void
		{
			var box:HBox=new HBox(mainSprite, 20, ypos);
			if (label)
			{
				var lb:Label=new Label();
				lb.text=label;
				lb.width=60;
				box.addChild(lb);
			}
			box.addChild(it);
			box.initialize();
			ypos+=30;
		}
		
		override public function show(event:Event=null):void
		{
			if(!this.parent){
				LayerManager.popupLayer.addChild(this);
				this.x = (stage.stageWidth-width)/2;
				this.y = (stage.stageHeight-height)/2 + 20;
			}
			super.show(event);
			
			if(this.isRegister){
				back.closeBtn2.visible = false;
				back.confirmBtn.enabled = false;
				
				base.cond={};
				
				var s:String = "注意到条件中的美誉度和友好度了吗？美誉度代表一个人的魅力度，友好度代表他对你有多少诚意。不想找错人吧？建议保持关注哦。";
				if(base.user.sex=='male'){
					s = "注意到条件中的美誉度和友好度了吗？美誉度代表一个人的魅力度，友好度代表你有多少诚意。想让女性更青睐自己吗？不断提升美誉度和对她的友好度吧。";
				}
				var myfun:Function = function():void{
					base.guideTip.getGuideTips(s,beginSetting,0,null,false);
				}
				TweenLite.delayedCall(1,myfun);	
			}else{
				back.closeBtn2.visible = true;
				back.confirmBtn.enabled = true;
			}
		}		
		
		private function beginSetting():void
		{
			back.confirmBtn.enabled = true;
			var s:String = "那么开始设定你的择友条件吧！";
			base.guideTip.getGuideTips(s);
		}
		
		private function finishSetting(obj:*):void
		{
			base.msg.showRollTipsMsg("成功完成操作");
			back.confirmBtn.enabled = false;
			base.cond = obj;
			var s:String = "升级能激活更多择友条件，想让遇到的人更适合自己吗？加油升级吧！";
			base.guideTip.getGuideTips(s,settingDone,3);
		}
		
		private function settingDone():void
		{
			base.guideTip.hideGuideTips();
			Dispatcher.dispatchEvent(new DataEvent(EventName.FinishFriendCond,{}));
			this.hide();
		}
		
		
		override public function destroy():void
		{
			if(base.settingCallback!=null){
				base.settingCallback=null;
			}
			back.confirmBtn.removeEventListener(MouseEvent.CLICK, onSubmit);
			back.closeBtn2.removeEventListener(MouseEvent.CLICK, hide);
			super.destroy();
		}

	}
}
